package com.css.fxfzxczh.modules.disasterSituationDataManage.emergencyReport.controller;

import com.alibaba.fastjson.JSONObject;
import com.css.fxfzxczh.base.response.RestResponse;
import com.css.fxfzxczh.modules.disasterSituationDataManage.emergencyReport.entity.EmergencyReport;
import com.css.fxfzxczh.modules.disasterSituationDataManage.emergencyReport.param.EmergencyReportParam;
import com.css.fxfzxczh.modules.disasterSituationDataManage.emergencyReport.service.EmergencyReportService;
import com.css.fxfzxczh.modules.disasterSituationDataManage.emergencyReport.vo.EmergencyReportVO;
import com.css.fxfzxczh.modules.synthesizeInfoManage.knowledgeManagement.entity.KnowledgeManagement;
import com.css.fxfzxczh.util.PlatformPageUtils;
import io.swagger.annotations.Api;
import io.swagger.v3.oas.annotations.Operation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @Author: zhangSongRui
 * @CreateTime: 2023/01/30
 * @Description: 灾情急报
 */
@RestController
@RequestMapping("/emergencyReport")
@Slf4j
@Api(tags = "灾情急报")
public class EmergencyReportController {
    @Resource
    EmergencyReportService emergencyReportService;

    /**
     * 分页查询
     *
     * @param queParams
     * @return
     */
    @GetMapping("/getPage")
    public RestResponse getPage(EmergencyReportParam queParams, HttpServletRequest request) {
        RestResponse restResponse = null;
        JSONObject jsonObject = new JSONObject();
        try {
            int curPage = Integer.valueOf(request.getParameter(PlatformPageUtils.CUR_PAGE));
            int pageSize = Integer.valueOf(request.getParameter(PlatformPageUtils.PAGE_SIZE));
            Map<String, Object> page = emergencyReportService.getPage(queParams, curPage, pageSize);
            jsonObject.put("message", "查询成功!");
            restResponse = RestResponse.succeed(page);
        } catch (Exception e) {
            String errormessage = "查询失败";
            log.error(errormessage, e);
            restResponse = RestResponse.fail(errormessage);
        }
        return restResponse;
    }

    @Operation(summary = "导出数据")
    @PostMapping("exportData")
    public void exportData(HttpServletResponse response,@RequestBody EmergencyReportParam param) {
        emergencyReportService.exportData(response, param);
    }

    @GetMapping("findById")
    @Operation(summary = "根据id查询")
    public RestResponse findById(String id) {
        try {
            EmergencyReportVO data = emergencyReportService.findById(id);
            return RestResponse.succeed(data);
        } catch (Exception e) {
            log.error("失败!", e);
            return RestResponse.fail("失败!");
        }
    }

    /**
     * 新增单条数据
     * @param param 参数
     * @return
     */
    @PostMapping("save")
    @Operation(summary = "新增数据")
    public RestResponse save(@RequestBody EmergencyReport param) {
        try {
            emergencyReportService.save(param);
            return RestResponse.succeed("新增成功");
        } catch (Exception e) {
            log.error("新增数据失败!", e);
            return RestResponse.fail("新增数据失败!");
        }
    }

    /**
     * 提交
     * @param id 数据id
     * @return
     */
    @GetMapping("submit")
    @Operation(summary = "提交")
    public RestResponse submit(String id) {
        try {
            emergencyReportService.submit(id);
            return RestResponse.succeed("提交成功");
        } catch (Exception e) {
            log.error("提交失败!", e);
            return RestResponse.fail("提交失败!");
        }
    }
    /**
     * 修改灾情急报
     *
     * @param emergencyReport
     * @return
     */
    @PostMapping("/update")
    @Operation(summary = "修改灾情急报")
    public RestResponse update(@RequestBody EmergencyReport emergencyReport) {
        RestResponse response = null;
        JSONObject jsonObject = new JSONObject();
        try {
            emergencyReportService.update(emergencyReport);
            jsonObject.put("message", "修改成功");
            response = RestResponse.succeed(jsonObject);
        } catch (Exception e) {
            String errormessage = e.getMessage();
            if(StringUtils.isBlank(errormessage)) {
                errormessage = "修改失败";
            }
            log.error(errormessage,e);
            response=RestResponse.fail(errormessage);
        }
        return response;
    }

    /***
     * 批量删除
     * @param
     * @return
     */
    @GetMapping("batchDelete")
    @Operation(summary = "批量删除灾情急报")
    public RestResponse batchDelete(String ids){
        RestResponse response = null;
        JSONObject jsonObject=new JSONObject();
        try{
            String[] split = ids.split(",");
            List<String> list = new ArrayList<>();
            for (String s : split) {
                list.add(s);
            }
            emergencyReportService.batchDelete(list);
            jsonObject.put("message","删除成功");
            response = RestResponse.succeed(jsonObject);
        }catch (Exception e){
            String errorMessage = "删除失败!";
            log.error(errorMessage,e);
            response = RestResponse.fail(errorMessage);
        }
        return response;
    }

    /**
     * 获取code
     */
    @GetMapping("/getCode")
    public RestResponse getCode(String eventCode) {
        RestResponse response = null;
        try {
            String code = emergencyReportService.getCode(eventCode);
            return RestResponse.succeed(code);
        } catch (Exception e){
            String errorMessage = "生成失败!";
            log.error(errorMessage,e);
            response = RestResponse.fail(errorMessage);
        }
        return response;

    }

}
